﻿using MedicalManagementAPP.AccountModule;
using MedicalManagementAPP.MyException;
using MedicalManagementAPP.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MedicalManagementAPP.Forms.DocForms
{
    public partial class PatientReceptionForm : Form
    {
        /// <summary>
        /// 以下三个变量用于储存当前选中病人、当前操作者（医生）、当前选中病历的ID
        /// </summary>
        Person patient;
        Person doc;
        string currentRecordSetID;

        /// <summary>
        /// 初始化窗口和其中的两个ListView控件，给操作者赋值（操作者是从登录界面传入的登陆者）
        /// </summary>
        /// <param name="doc">登陆者</param>
        public PatientReceptionForm(ref Person doc)
        {
            InitializeComponent();
            this.doc = doc;
            initPatientListView();
            initDetailList();
        }
        private void initDetailList()
        {
            detailListView.Columns.Add("病历ID");
            detailListView.Columns[0].Width = 50;
            detailListView.Columns.Add("医生工号");
            detailListView.Columns[1].Width = 80;
            detailListView.Columns.Add("写入日期");
            detailListView.Columns[2].Width = 130;
            detailListView.Columns.Add("简述");
            detailListView.Columns[3].Width = 300;
        }
        private void initPatientListView()
        {
            patientsListView.Columns.Add("病人ID");
            patientsListView.Columns[0].Width = 50;
            patientsListView.Columns.Add("姓名");
            patientsListView.Columns[1].Width = 80;
            patientsListView.Columns.Add("手机号");
            patientsListView.Columns[2].Width = 100;
            patientsListView.Columns.Add("注册日期");
            patientsListView.Columns[3].Width = 140;
            refreshPatientListView();
        }

        /// <summary>
        /// 通过读取数据库来刷新病人列表
        /// </summary>
        private void refreshPatientListView()
        {
            DataTable dt = PatientManager.GetAllPatient();
            patientsListView.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListViewItem item = new ListViewItem();
                item.Text = row[0].ToString();
                item.SubItems.Add(row[1].ToString());
                item.SubItems.Add(row[2].ToString());
                item.SubItems.Add(row[3].ToString());
                patientsListView.Items.Add(item);
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            if (patientTextBox.Text == "")
            {
                MessageBox.Show("请输入病人ID");
                return;
            }
            try { refreshDetailListBox(patientTextBox.Text); }
            catch(PersonNotFound) { MessageBox.Show("没有找到该患者"); patientTextBox.Text = ""; }
            

        }

        private void label2_Click(object sender, EventArgs e)
        {
            refreshPatientListView();
        }

        /// <summary>
        /// 选中病人时，更新patient变量，同时查询该病人的病历记录，显示在detailListView中
        /// </summary>
        private void patientsListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            if(patientsListView.SelectedItems.Count < 1) { return; }
            string id = patientsListView.SelectedItems[0].Text;
            string name = patientsListView.SelectedItems[0].SubItems[1].Text;
            patient = new Person(name, "Patient", id);
            patientTextBox.Text = id;
            refreshDetailListBox(id);
            describeRichTextBox.Clear();
            curPatientLabel.Text = "当前选中病人号：" + patient.PId;
        }
        /// <summary>
        /// 通过读取数据库来刷新当前病人的病历列表
        /// </summary>
        private void refreshDetailListBox(string id)
        {
            DataTable recordSets = PatientManager.GetRecordSetsByID(id);
            detailListView.Items.Clear();
            if (recordSets == null) { throw new PersonNotFound(id); }
            foreach (DataRow row in recordSets.Rows)
            {
                // 顺序：ID 手机号 姓名 注册日期
                ListViewItem item = new ListViewItem();
                item.Text = row[0].ToString();
                item.SubItems.Add(row[1].ToString());
                item.SubItems.Add(row[2].ToString());
                item.SubItems.Add(row[3].ToString());
                detailListView.Items.Add(item);
            }
        }

        /// <summary>
        /// 选中病历时，更新currentRecordSetID，同时查询该次病历的描述，显示在describeRichTextBox中
        /// </summary>
        private void detailListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (detailListView.SelectedItems.Count < 1) { return; }
            if (patient == null) { MessageBox.Show("请先选择患者");return; }
            string id = detailListView.SelectedItems[0].Text;

            describeRichTextBox.Text = PatientManager.GetDescribeByID(id);
            currentRecordSetID = id;
            curIdLabel.Text = "当前修改病历号："+id;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //RecordSetManager.CreateRecordSetByID(doc.PId, patient.PId);
            //MessageBox.Show("创建成功");
            refreshDetailListBox(patient.PId);
        }

        private void 确认更改ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (currentRecordSetID == null) { MessageBox.Show("请先选择病历"); return; };
            if (RecordSetManager.UpdateRecordDescribe(currentRecordSetID, describeRichTextBox.Text))
            {
                MessageBox.Show("修改成功");
                refreshDetailListBox(patient.PId);
            }
            else
            {
                MessageBox.Show("修改失败");
            }
        }
    }
}
